// goods-pay.js
var iFDU = require('../../utils/iFDU.js');
var app = getApp();
Page({

  data: {
    config: app.config,
    globalData: app.globalData,
    showModal: false,
    showRedbag: false,
    showQRCode: false,
    orderId: 0,
    redbagId: 0,
    prepayInfo: {},
  },

  openConfirm: function () {
    wx.showModal({
      title: '弹窗标题',
      content: '弹窗内容，告知当前状态、信息和解决方法，描述文字尽量控制在三行内',
      confirmText: "主操作",
      cancelText: "辅助操作",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          wx.navigateTo({
            url: '../pay-success/pay-success',
          })
        } else {
          console.log('用户点击辅助操作')
        }
      }
    });
  },

  openAlert: function () {
    // wx.showModal({
    //   content: '<image src="" ></image>',
    //   showCancel: false,
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     }
    //   }
    // });
    this.showRedbag();
  },

  showCustomDialog: function () {
    this.setData({
      showModal: true
    })
  },

  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    });

    var that = this;

    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      var data = {
        userId: globalData.userId,
        orderId: that.data.orderId,
      };

      if (that.data.redbagId > 0) {
        data['redbagId'] = that.data.redbagId;
      }
      iFDU.api.doGet('payment/getInfoForPayment', data, function (data) {
        that.setData({
          prepayInfo: data
        });
      });
    });
  },

  onShow: function () {
    

    
  },

  preventTouchMove: function () {
  },

  hideModal: function () {
    this.setData({
      showModal: false
    });
  },

  onCancel: function () {
    this.hideModal();
  },

  onConfirm: function () {
    this.hideModal();
  },

  payByBanlance: function (e) {
    var that = this;
    this.setData({
      showModal: true
    });
  },

  doPayByBalance: function (e) {
    var that = this;
    var formId = e.detail.formId;
    var data = {
      userId: that.data.globalData.userId,
      orderId: that.data.orderId,
      formId: formId
    };

    if (that.data.redbagId > 0) {
      data['redbagId'] = that.data.redbagId;
    }
    iFDU.api.doGet('payment/paymentByAmount', data, function (data) {
      if (data.virRedBagId && data.virRedBagId > 0) {
        wx.redirectTo({
          url: '../pay-success/pay-success?orderId=' + that.data.orderId + '&virRedBagId=' + data.virRedBagId,
        })
      } else {
        wx.redirectTo({
          url: '../pay-success/pay-success?orderId=' + that.data.orderId,
        })
      }
      
    });
  },

  payByWx: function (e) {
    var formId = e.detail.formId;
    var that = this;
    var data = {
      userId: that.data.globalData.userId,
      orderId: that.data.orderId,
      formId: formId
    };

    if (that.data.redbagId > 0) {
      data['redbagId'] = that.data.redbagId;
    }
    iFDU.api.doGet('payment/paymentByWx', data, function (data) {
      var payInfo = data;
      var isNeedPay = payInfo.isNeedPay;
      if (isNeedPay == 0) {
        var virRedBagId = payInfo.virRedBagId;
        if (virRedBagId > 0) {
          wx.redirectTo({
            url: '../pay-success/pay-success?orderId=' + that.data.orderId + '&virRedBagId=' + virRedBagId,
          })
        } else {
          wx.redirectTo({
            url: '../pay-success/pay-success?orderId=' + that.data.orderId,
          })
        }
        return;
      }
      wx.requestPayment({
        timeStamp: payInfo.timeStamp,
        nonceStr: payInfo.nonceStr,
        package: payInfo.package,
        signType: payInfo.signType,
        paySign: payInfo.paySign,
        success: function (res) {
          if (payInfo.virRedBagId && payInfo.virRedBagId > 0) {
            wx.redirectTo({
              url: '../pay-success/pay-success?orderId=' + that.data.orderId + '&virRedBagId=' + payInfo.virRedBagId,
            })
          } else {
            wx.redirectTo({
              url: '../pay-success/pay-success?orderId=' + that.data.orderId,
            })
          }
          
        },
        fail: function (res) {
          iFDU.toast.showToast({
            title: '支付失败，请再试',
            mask: false
          })
        }
      })
    });
  },

  payByBanlanceAndWx: function (e) {
    var formId = e.detail.formId;
    var that = this;
    var data = {
      userId: that.data.globalData.userId,
      orderId: that.data.orderId,
      formId: formId
    };

    if (that.data.redbagId > 0) {
      data['redbagId'] = that.data.redbagId;
    }
    iFDU.api.doGet('payment/paymentByWxAndAmount', data, function (data) {
      var payInfo = data;
      var isNeedPay = payInfo.isNeedPay;
      if (isNeedPay == 0) {
        var virRedBagId = payInfo.virRedBagId;
        if (virRedBagId > 0) {
          wx.redirectTo({
            url: '../pay-success/pay-success?orderId=' + that.data.orderId + '&virRedBagId=' + virRedBagId,
          })
        } else {
          wx.redirectTo({
            url: '../pay-success/pay-success?orderId=' + that.data.orderId,
          })
        }
        return;
      }
      wx.requestPayment({
        timeStamp: payInfo.timeStamp,
        nonceStr: payInfo.nonceStr,
        package: payInfo.package,
        signType: payInfo.signType,
        paySign: payInfo.paySign,
        success: function (res) {
          if (payInfo.virRedBagId && payInfo.virRedBagId > 0) {
            wx.redirectTo({
              url: '../pay-success/pay-success?orderId=' + that.data.orderId + '&virRedBagId=' + payInfo.virRedBagId,
            })
          } else {
            wx.redirectTo({
              url: '../pay-success/pay-success?orderId=' + that.data.orderId,
            })
          }
        },
        fail: function (res) {
          iFDU.toast.showToast({
            title: '支付失败，请再试',
            mask: false
          })
        }
      })
    });
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})